import { Module, MiddlewareConsumer } from '@nestjs/common'
import { APP_GUARD, APP_PIPE, APP_FILTER } from '@nestjs/core'
import { ConfigModule } from '@nestjs/config'
import { LoggerMiddleware } from '@/common/middlewares/logger.middleware'
import { JwtAuthGuard } from '@/modules/auth/guards/jwtAuth.guard'
import { UserPipe } from '@/common/pipes/user.pipe'
import { UserModule } from '@/modules/user/user.module'
import { RedisModule } from '@/modules/redis/redis.module'
import { AuthModule } from '@/modules/auth/auth.module'
import { MenuModule } from '@/modules/menu/menu.module'

import { AllExceptionsFilter } from '@/common/filters/allExceptions.filter'
import Config from '@/config'

@Module({
	imports: [
		ConfigModule.forRoot({
			isGlobal: true,
			expandVariables: true,
			envFilePath: [`.env.${process.env.NODE_ENV}`, '.env'],
			load: [...Object.values(Config)],
		}),
		RedisModule,
		UserModule,
		AuthModule,
		MenuModule,
	],
	providers: [
		{
			provide: APP_GUARD,
			useClass: JwtAuthGuard,
		},
		{
			provide: APP_PIPE,
			useClass: UserPipe,
		},
		{
			provide: APP_FILTER,
			useClass: AllExceptionsFilter,
		},
	],
})
export class AppModule {
	configure(consumer: MiddlewareConsumer) {
		consumer.apply(LoggerMiddleware).forRoutes('*')
	}
}
